#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , scene(new QGraphicsScene(this))
    , pixmapItem(nullptr)
{
    ui->setupUi(this);
    setWindowTitle("绘图助手");
    pixmap.load(":/new/prefix1/24.jpg");
    pixmapItem = new QGraphicsPixmapItem(pixmap);
    scene->addItem(pixmapItem);
    scene->setSceneRect(0, 0, 901, 474);
    ui->graphicsView->setScene(scene);

    // 注意这里连接时使用了 QPointF
    connect(ui->graphicsView, &CustomGraphicsView::mouseClicked, this, &MainWindow::onMouseClicked);
    connect(ui->graphicsView, &CustomGraphicsView::mouseMoved, this, &MainWindow::onMouseMoved);
    connect(ui->graphicsView, &CustomGraphicsView::mouseReleased, this, &MainWindow::onMouseReleased);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onMouseClicked(const QPointF &pos) {
    QString text = QString("clicked: (%1, %2)").arg(pos.x()).arg(pos.y());
    ui->mouse_press->setText(text);
    qDebug() << "Mouse clicked at:" << pos;
}

void MainWindow::onMouseMoved(const QPointF &pos) {
    QString text = QString("moved: (%1, %2)").arg(pos.x()).arg(pos.y());
    ui->mouse_move->setText(text);
    qDebug() << "Mouse moved to:" << pos;
}

void MainWindow::onMouseReleased(const QPointF &pos) {
    QString text = QString("released: (%1, %2)").arg(pos.x()).arg(pos.y());
    ui->mouse_release->setText(text);
    qDebug() << "Mouse released at:" << pos;
}



void MainWindow::on_pushButton_paint_rect_clicked()
{
    ui->graphicsView->setDrawMode(CustomGraphicsView::RectMode);
}

void MainWindow::on_pushButton_paint_line_clicked()
{
    ui->graphicsView->setDrawMode(CustomGraphicsView::LineMode);
}


void MainWindow::on_pushButton_paint_ellipse_clicked()
{
    ui->graphicsView->setDrawMode(CustomGraphicsView::EllipseMode);
}

